<?php

namespace app\admin\controller;

use app\admin\BaseController;

class GoodsController extends BaseController
{
    /**
     * 获取商品列表
     * @return \think\response\Json
     */
    public function listsAction()
    {
        try{
            $post = $this->get;

            $params['order'] = ['goodsId'=>'desc'];

            if(!empty($post['goodsName'])){
                $params['where'][] = ['goodsName','like','%'.$post['goodsName'].'%'];
            }
            $res = $this->app->goodsService->getLists($params, $this->page, $this->pagesize);

            $data['lists'] = [];
            foreach($res as $key=>$vo){
                $arr = [];
                $arr['goodsId'] = $vo->goodsId;
                $arr['goodsName'] = $vo->goodsName;
                $arr['categoryId'] = $vo->categoryId;
                $arr['categoryName'] = $vo->getCategoryName();
                $arr['thumb'] = $vo->thumb;
                $arr['marketPrice'] = $vo->marketPrice;
                $arr['credit'] = $vo->credit;
                $arr['stock'] = $vo->stock;
                $arr['viewCount'] = $vo->viewCount;
                $arr['saleCount'] = $vo->saleCount;
                $arr['clickCount'] = $vo->clickCount;
                $arr['sort'] = $vo->sort;
                $arr['status'] = $vo->status;
                $arr['statusText'] = $vo->getStatusText();
                $arr['createdAt'] = $vo->createdAt;
                $data['lists'][] = $arr;
            }

            $data['totalInfo'] = $this->app->utilService->getMeta($res);

            return json(['code' => 200, 'msg' => '查询成功', 'data'=> $data]);
        }catch(\Exception $e){
            return json(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data'=> []]);
        }
    }

    /**
     * @Description 详情
     * @param  [type] $id
     *
     * @return void
     */
    public function detailAction($id)
    {
        try{
            $goods = $this->app->goodsService->getDetail($id);
            if(!$goods){
                throw new \Exception("商品不存在",500);
            }
            $data = $goods->toArray();
            $data['models'] = $goods->getModelIds();

            return json(['code' => 200, 'msg' => '查询成功', 'data'=> $data]);
        }catch(\Exception $e){
            return json(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data'=> []]);
        } 
    }

    public function getModelsAction()
    {
        try{
            $models = $this->app->goodsService->getModels();

            return json(['code' => 200, 'msg' => '查询成功', 'data'=> $models]);
        }catch(\Exception $e){
            return json(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data'=> []]);
        }
    }

    /**
     * 添加商品
     * @return \think\response\Json
     */
    public function addAction()
    {
        try{
            if(empty($this->post)){
                throw new \Exception("没有数据提交，不能处理",500);
            }
            $this->post['adminId'] = $this->admin->adminId;
            $this->post['appletId'] = $this->appletId;
            $this->app->goodsService->add($this->post);

            return json(['code' => 200, 'msg' => '添加成功', 'data'=> []]);
        }catch(\Exception $e){
            return json(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data'=> []]);
        }
    }

    /**
     * @Description 名录修改
     *
     * @return void
     */
    public function updateAction($id)
    {
        try {
            $goods = $this->app->goodsService->getDetail($id);

            $this->app->goodsService->update($goods, $this->post);

            return json(['code' => 200, 'msg' => '操作成功', 'data'=> []]);
        } catch (\Exception $e) {
            return json(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data'=> []]);
        }
    }

    /**
     * @param $id
     * @return \think\response\Json
     */
    public function deleteAction($id)
    {
        try {
            $goods = $this->app->goodsService->getDetail($id);

            $this->app->questionService->delete($goods);

            return json(['code' => 200, 'msg' => '操作成功', 'data'=> []]);
        } catch (\Exception $e) {
            return json(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data'=> []]);
        }
    }

    /**
     * @param $id
     * @return \think\response\Json
     */
    public function enableAction($id)
    {
        try {
            $goods = $this->app->goodsService->getDetail($id);

            $this->app->goodsService->enable($goods);

            return json(['code' => 200, 'msg' => '操作成功', 'data'=> []]);
        } catch (\Exception $e) {
            return json(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data'=> []]);
        }
    }

    /**
     * @param $id
     * @return \think\response\Json
     */
    public function disableAction($id)
    {
        try {
            $goods = $this->app->goodsService->getDetail($id);

            $this->app->goodsService->disable($goods);

            return json(['code' => 200, 'msg' => '操作成功', 'data'=> []]);
        } catch (\Exception $e) {
            return json(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data'=> []]);
        }
    }
}